package com.blue.fish.tiny.parser.ast;

import com.blue.fish.tiny.lexer.Token;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

/**
 * @author QingFan
 * @version 1.0.0
 * @date 2023年01月30日 21:23
 */
public abstract class ASTNode {

    protected ArrayList<ASTNode> children = new ArrayList<>();
    protected ASTNode parent;

    protected Token lexeme; // 词法单元
    protected String label; // 备注
    protected ASTNodeTypes type; // 类型

    public ASTNode() {
    }

    public ASTNode(ASTNode parent) {
        this.parent = parent;
    }

    public ASTNode(ASTNode parent, ASTNodeTypes type, String label) {
        this.parent = parent;
        this.label = label;
        this.type = type;
    }

    public ASTNode getChild(int index) {
        return this.children.get(index);
    }

    public void addChild(ASTNode node) {
        this.children.add(node);
    }

    public Token getLexeme() {
        return lexeme;
    }

    public void setLexeme(Token lexeme) {
        this.lexeme = lexeme;
    }
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public ASTNodeTypes getType() {
        return type;
    }

    public void setType(ASTNodeTypes type) {
        this.type = type;
    }

    public ArrayList<ASTNode> getChildren() {
        return children;
    }

    public void print(int indent) {
        if(indent == 0) {
            System.out.println("print:" + this);
        }

        System.out.println(StringUtils.leftPad(" ", indent *2) + label);
        for(var child : children) {
            child.print(indent + 1);
        }
    }
}
